#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests
from requests.exceptions import HTTPError

r = requests.get('https://www.douban.com/') # 豆瓣首页
print('status：', r.status_code)
# Status Codes
""" 
for url in ['https://api.github.com', 'https://api.github.com/invalid']:
    try:
        response = requests.get(url)

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!') 
"""
# Query String Parameters
response = requests.get(
    'https://api.github.com/search/repositories',
    params={'q': 'requests+language:python'},
)

# Inspect some attributes of the `requests` repository
json_response = response.json()
repository = json_response['items'][0]
print(f'Repository name: {repository["name"]}')  # Python 3.6+
print(f'Repository description: {repository["description"]}')  # Python 3.6+



# Request Headers

response = requests.get(
    'https://api.github.com/search/repositories',
    params={'q': 'requests+language:python'},
    headers={'Accept': 'application/vnd.github.v3.text-match+json'},
)

# View the new `text-matches` array which provides information
# about your search term within the results
json_response = response.json()
repository = json_response['items'][0]
print(f'Text matches: {repository["text_matches"]}')
# print(response.content)
print(response.headers)

""" 
params = {'key': 'value'}
r = requests.post(url, json=params) # 内部自动序列化为JSON
"""

""" 
upload_files = {'file': open('report.xls', 'rb')}
r = requests.post(url, files=upload_files)
"""

""" 
 r.cookies['ts']
"""

# r = requests.post('https://accounts.douban.com/login', data={'form_email': 'abc@example.com', 'form_password': '123456'})